<?php

/**
 * 快递vip折扣
 */
class Application_Model_ExpressPricing extends Application_Model_DomainObject{

    protected $_id;          //ID
    protected $_expressId;   //关联uuch_express.id  快递id
    protected $_countryId;   //关联uuch_city_new.id 国家id
    protected $_type;        //1续重2公斤
    protected $_start;       //开始重量段
    protected $_end;         //结束重量段
    protected $_price;       //价格

    const TYPE_CONTINUED = 1;   //续重
    const TYPE_KG = 2;          //公斤

    /**
     * 获取数据
     * @return list
    */
    public static function getAll($condition = array())
    {
        if(!is_array($condition)){
            return false;
        }
        return self::getDao()->queryAll($condition , ' type asc ');
    }

    /**
     * 查询单条记录
    */
    public static function getInfo($condition = array())
    {
        if(!is_array($condition)){
            return false;
        }
        return self::getDao()->queryOne($condition);
    }

    /**
     * @desc    获取邮费
     * @param   $expressId  快递id
     *          $cityId     国家id
     *          $weight     重量 g 克
     * warning: 均未加入偏远费 
    */
    public static function getFreight($expressId = 0, $cityId = 0, $weight = 0)
    {
        $re = array(
                'status'     => -1, //状态码
                'expressFee' => 0,    //快递费
                'fuelFee'    => 0,    //燃油费
                'remoteFee'  => 0,    //偏远费
                'othersFee'  => 0,    //其他费
                'totalFee'   => 0,    //总费用
            );
        if(!(empty($expressId) || empty($cityId) || empty($weight) || !is_numeric($expressId) || !is_numeric($cityId) || !is_numeric($weight))){
            //获取快递信息
            $express   = Application_Model_Express::load($expressId);
            if(!empty($express)){
                //获取国家信息
                $country   = Application_Model_city::load($cityId);
                if(!empty($country)){
                    //获取快递对应国家价格
                    $exPrice   = Application_Model_ExpressPrice::getDao()->queryOne(array('expressId' => $expressId, 'cityId' => $cityId), 'id asc');
                    if($exPrice !== false){
                        //获取重量段价格
                        $expring   = Application_Model_ExpressPricing::getDao()->queryAll(array('expressId' => $expressId, 'countryId' => $cityId), 'type asc');
                        //各价格段数据
                        $priceing = array(
                                    'pricexz' => array(), 
                                    'pricegj' => array(), 
                                );

                        foreach ($expring as $key => $pricing) {
                            $tmp = array(
                                    'start' => $pricing->Start,
                                    'end'   => $pricing->End,
                                    'price' => $pricing->Price,
                                );

                            if($pricing->Type == 1){
                                $priceing['pricexz'][] = $tmp;
                            }else{
                                $priceing['pricegj'][] = $tmp;
                            }
                        }
                        $pricekg = 0;//每公斤价格
                        // $weightgj = ceil($weight/1000);//向上取整转为公斤
                        $weightgj = $weight/1000;//向上取整转为公斤
                        if($exPrice->IsWeight == 1 && $weightgj > $exPrice->Beyond && !empty($priceing['pricegj']) ){
                            foreach ($priceing['pricegj'] as $key => $p) {
                                if($p['start'] < $weightgj && $weightgj <= $p['end']){
                                    $pricekg = $p['price'];
                                    break;
                                }
                            }
                        }
                        if($pricekg > 0){
                            $weightgj = ceil($weight/1000);//向上取整转为公斤
                            //按公斤计费
                            $re = array(
                                'status'     => 0,    //状态码
                                'expressFee' => $weightgj * $pricekg,    //快递费
                            );
                        }else{
                            if($exPrice->FirstWeight >= $weight){
                                //返回首重价格
                                $re = array(
                                    'status'     => 0,      //状态码
                                    'expressFee' => $exPrice->FirstPrice,    //快递费
                                );
                            }else{
                                // $weightxz = ceil($weight/1000);//向上取整转为公斤
                                $weightxz = $weight/1000;//向上取整转为公斤
                                foreach ($priceing['pricexz'] as $key => $p) {
                                    if($p['start'] < $weightxz && $weightxz <= $p['end']){
                                        // echo $p['start'] .'<='. $weightxz .'&&'. $weightxz .'<'. $p['end'];exit;
                                        $pricekg = $p['price'];
                                        break;
                                    }
                                }
                                if($pricekg > 0){
                                    //首重价格 +  (续重次数 * 续重价格)
                                    // echo $exPrice->FirstPrice .'+'. ceil(($weight - $exPrice->FirstWeight) / $exPrice->SubsequentWeight) .'*'. $pricekg . '<br/>';exit;

                                    //叠加
                                    $ppxz = 0;
                                    foreach ($priceing['pricexz'] as $k => $p) {
                                        if($weight > $p['end'] * 1000){
                                            if($p['start'] == 0){
                                                $ppxz += $exPrice->FirstPrice;
                                                $ppxz += ceil(($p['end']*1000 - $exPrice->FirstWeight) / $exPrice->SubsequentWeight) * $p['price'];
                                            }else{
                                                $ppxz += ceil(($p['end'] - $p['start']) *1000 / $exPrice->SubsequentWeight) * $p['price'];
                                            }
                                        }

                                        if($weight > $p['start'] * 1000 && $weight <= $p['end'] * 1000){
                                            if($p['start'] == 0){
                                                $ppxz += $exPrice->FirstPrice;
                                                $ppxz += ceil(($weight - $exPrice->FirstWeight)/$exPrice->SubsequentWeight)* $p['price'];
                                            }else{
                                                $ppxz += ceil(($weight - $p['start']*1000)/$exPrice->SubsequentWeight)* $p['price'];
                                            }
                                            
                                        }
                                    }

                                    $re = array(
                                        'status'     => 0,      //状态码
                                        'expressFee' => $ppxz,    //快递费
                                        // 'expressFee' => $exPrice->FirstPrice + ceil(($weight - $exPrice->FirstWeight) / $exPrice->SubsequentWeight) * $pricekg,    //快递费
                                    );
                                }else{
                                    $re = array(
                                        'status'     => 46,      //未找到重量对应价格
                                    );
                                }
                            }
                        }
                    }else{
                        $re = array(
                            'status'     => 45,      //未设置快递对应国家信息
                        );
                    }
                }else{
                    $re = array(
                        'status'     => 43,      //国家不存在
                    );
                }
            }else{
                $re = array(
                    'status'     => 42,      //快递不存在
                );
            }
        }else{
            $re = array(
                'status'     => 41,      //参数异常
            );
        }
        if($re['status'] < 40){
            $expressFee = $re['expressFee'];
            $re['fuelFee']   = $expressFee * $express->FuelSurcharge;    //燃油费
            $re['remoteFee'] = $express->RemoteFee;    //偏远费
            $re['othersFee'] = $express->OthersFee;    //其他费
            //未添加偏远费   + $express->RemoteFee 
            $re['totalFee']  = $expressFee + $expressFee * $express->FuelSurcharge + $express->OthersFee;    //总费用
        }
        return $re;
    }
}